package com.starblink.android.basic.widget.refresher

import android.annotation.SuppressLint
import android.content.Context
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import com.airbnb.lottie.LottieAnimationView
import com.airbnb.lottie.LottieDrawable
import com.scwang.smart.refresh.layout.api.RefreshFooter
import com.scwang.smart.refresh.layout.api.RefreshHeader
import com.scwang.smart.refresh.layout.api.RefreshKernel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.constant.RefreshState
import com.scwang.smart.refresh.layout.constant.SpinnerStyle
import com.starblink.android.basic.R
import com.starblink.android.basic.ext.color
import com.starblink.android.basic.ext.gone
import com.starblink.android.basic.ext.invisible
import com.starblink.android.basic.ext.visible
import com.starblink.android.basic.extension.dp2px

/**
 * SKRefreshFooter2:
 * 相比SKRefreshFooter,修改endView样式
 */
@SuppressLint("RestrictedApi")
class SKRefreshFooter2 constructor(val context: Context,
                                   /**
                                    * 隐藏BottomLine
                                    */
                                   var noBottomLine: Boolean = false) : RefreshFooter {

    private val lottieView: LottieAnimationView = LottieAnimationView(context).apply {
        layoutParams = FrameLayout.LayoutParams(100.dp2px(), 50.dp2px()).also {
            it.gravity = Gravity.CENTER
        }
        setAnimation("lottie/refresh_2.json")
        repeatCount = LottieDrawable.INFINITE
        repeatMode = LottieDrawable.RESTART
    }

    private val endView: View
    private val view: View


    init {
        endView = LayoutInflater.from(context).inflate(R.layout.layout_skrefresher_end_view, null)

        view = FrameLayout(context).apply {
            layoutParams = ViewGroup.MarginLayoutParams(
                ViewGroup.MarginLayoutParams.MATCH_PARENT,
                100.dp2px()
            )
            addView(lottieView)
            addView(endView)
        }
        endView.gone()
    }

    override fun onStateChanged(
        refreshLayout: RefreshLayout,
        oldState: RefreshState,
        newState: RefreshState
    ) {
        when (newState) {
            RefreshState.PullUpToLoad, RefreshState.ReleaseToLoad, RefreshState.Loading, RefreshState.LoadReleased, RefreshState.LoadFinish -> {
                if (lottieView.isAnimating.not()) {
                    lottieView.playAnimation()
                }
            }
            else -> {
                lottieView.pauseAnimation()
            }
        }
    }

    override fun getView(): View = view

    override fun getSpinnerStyle(): SpinnerStyle = SpinnerStyle.Translate

    override fun setPrimaryColors(vararg colors: Int) {
    }

    override fun onInitialized(kernel: RefreshKernel, height: Int, maxDragHeight: Int) {
    }

    override fun onMoving(
        isDragging: Boolean,
        percent: Float,
        offset: Int,
        height: Int,
        maxDragHeight: Int
    ) {

    }

    override fun onReleased(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {

    }

    override fun onStartAnimator(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {

    }

    override fun onFinish(refreshLayout: RefreshLayout, success: Boolean): Int = 0

    override fun onHorizontalDrag(percentX: Float, offsetX: Int, offsetMax: Int) {
    }

    override fun isSupportHorizontalDrag(): Boolean = false

    override fun setNoMoreData(noMoreData: Boolean): Boolean {
        if (noMoreData) {
            lottieView.gone()
            if (noBottomLine) {
                endView.invisible()
            } else {
                endView.visible()
            }
        } else {
            lottieView.visible()
            endView.gone()
        }
        return true
    }

}